package com.android.lovely.ui.dialog

import android.content.Intent
import android.net.Uri
import androidx.core.view.isVisible
import com.android.lovely.R
import com.android.lovely.databinding.DialogUpgradeBinding
import com.android.lovely.framework.BaseDialogFragment
import com.android.lovely.manager.KVStore
import com.kotlin.extensions.view.ViewExt.clickable

/**
 *
 * @author why
 * @date 2025/3/11 11:30
 */
class AppUpgradeDialog : BaseDialogFragment<DialogUpgradeBinding>() {


    override fun initializeViews() {
        val detail = KVStore.system?.guidDetail ?: run {
            dismiss()
            return
        }
        if (detail.up == 1) {
            binding.ivClose.isVisible = detail.mup == 0
            binding.confirm.text = detail.updetail?.bt ?: getString(R.string.confirm)
            binding.content.text = detail.updetail?.des?: getString(R.string.upgrade_tip)
            isCancelable = detail.mup == 0
        } else {
            dismiss()
            return
        }
        binding.ivClose.clickable { dismissAllowingStateLoss() }
        binding.confirm.clickable {
            if(detail.updetail?.ll.isNullOrEmpty()) {
                dismissAllowingStateLoss()
            } else {
                runCatching {
                    val browserIntent = Intent(Intent.ACTION_VIEW)
                    browserIntent.data = Uri.parse(detail.updetail?.ll)
                    startActivity(browserIntent)
                }
            }
        }
    }
}